package com.kacheap.handler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import com.kacheap.model.MessageModel;
import com.lmax.disruptor.EventHandler;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HelloEventHandler implements EventHandler<MessageModel> {

    //登录用户token
    private static String token = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJoc3JxcmwiLCJjcmVhdGVkIjoxNzAxNzQ0ODI1NDU3LCJ0aW4iOiI5MTE1MDEwME1BME1YSkNZM0MiLCJhcHBsaWNhdGlvbklkIjoiMSIsImV4cCI6MTcwMTc0ODQyNSwib3JnSWQiOiI1IiwidXNlcm5hbWUiOiLlkbzlkozmtannibnluILnh4PmsJTng63lipvmnInpmZDlhazlj7gifQ.w3ODIcyOVclrFhnIWLEJQikSqgt-_fZExRc_SBZ3laEKg6FxBjcKcX7LHeFPbmZaqAl9tQ8nUG235nNcOSqQCQ";

    @Override
    public void onEvent(MessageModel event, long sequence, boolean endOfBatch) {
        try {
            if (null == event) {
                return;
            }

            TimeUnit.SECONDS.sleep(1);
            // TODO 这里添加业务功能代码
            uploadInvoice();
            log.info(Thread.currentThread().getId() + " ==> 消费者消费的信息是：{}", event);
        } catch (Exception e) {
            log.info("消费者处理消息失败");
        }

    }

    public static void uploadInvoice() {
        try {
            String headUrl = "http://114.116.222.38:32263";
            //headUrl = "http://127.0.0.1:8060";
            String httpUrl = headUrl + "/api/question/invoice/setAlarmValue";
            String invoiceUrl = headUrl + "/api/question/toThird/invoiceupload";

            long start = System.currentTimeMillis();
            // 创建请求正文
            String requestBody = "{\n" +
                    "  \"nsrsbh\": \"911101150612810428\",\n" +
                    "  \"fpslyjsz\": 50,\n" +
                    "  \"fpedyjsz\": 5000.00\n" +
                    "}";

            // 创建URL对象
            URL url = new URL(invoiceUrl);
            // 创建HttpURLConnection对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 设置请求方法为POST
            connection.setRequestMethod("POST");
            // 添加Token到请求头
            connection.setRequestProperty("Authorization", token);

            // 设置其他请求头参数
            connection.setRequestProperty("Content-Type", "application/json");

            // 允许写入请求正文
            connection.setDoOutput(true);
            int i = (int) Math.random() * 99;
            Random r = new Random();
            //生成随机数
            int num = r.nextInt(1000000000);

            Long startNbr = 2023101112312312319L + i;
            System.out.println("受理流水号：" + num);
            //上传发票请求正文
            requestBody = "{\n" +
                    "  \"method\": \"fpkj\",\n" +
                    "  \"object\": {\n" +
                    "    \"fpqqlsh\": " + num + ",\n" +
                    "    \"lzfpbz\": \"0\",\n" +
                    "    \"fppz\": \"01\",\n" +
                    "    \"gmfzrrbz\": \"N\",\n" +
                    "    \"tdys\": \"\",\n" +
                    "    \"cezslxDm\": \"01\",\n" +
                    "    \"sgfplxDm\": \"\",\n" +
                    "    \"ckywsyzcDm\": \"\",\n" +
                    "    \"zzsjzjtDm\": \"\",\n" +
                    "    \"xsfnsrsbh\": \"91110108MA01E78G61\",\n" +
                    "    \"xsfmc\": \"北京众信链科技有限责任公司" + i + "\",\n" +
                    "    \"xsfdz\": \"北京市顺义区马坡镇聚源西路7号内215号\",\n" +
                    "    \"xsfdh\": \"13910094945\",\n" +
                    "    \"xsfkhh\": \"北京农商银行\",\n" +
                    "    \"xsfzh\": \"12310108MA01E78159\",\n" +
                    "    \"gmfnsrsbh\": \"91110106MA04F9LJ6K" + i + "\",\n" +
                    "    \"gmfmc\": \"北京票小报科技有限责任公司\",\n" +
                    "    \"gmfdz\": \"北京市海淀区清河园永泰甲1号综合楼2层246号\",\n" +
                    "    \"gmfdh\": \"13910094945\",\n" +
                    "    \"gmfkhh\": \"北京银行\",\n" +
                    "    \"gmfzh\": \"12310108MA01E78456\",\n" +
                    "    \"gmfjbr\": \"小王\",\n" +
                    "    \"jbrsfzjhm\": \"110108197507041122\",\n" +
                    "    \"gmfjbrlxdh\": \"13711325618\",\n" +
                    "    \"hjje\": \"1762.87\",\n" +
                    "    \"hjse\": \"14.23\",\n" +
                    "    \"jshj\": \"1777.10\",\n" +
                    "    \"skyhmc\": \"北京农商行\",\n" +
                    "    \"skyhzh\": \"12310108MA01E78159\",\n" +
                    "    \"jsfs\": \"01\",\n" +
                    "    \"ysxwfsd\": \"\",\n" +
                    "    \"kpr\": \"李小二\",\n" +
                    "    \"kprzjhm\": \"110108197707042266\",\n" +
                    "    \"kprzjlx\": \"\",\n" +
                    "    \"dylzfphm\": \"\",\n" +
                    "    \"hzqrxxdbh\": \"\",\n" +
                    "    \"hzqrduuid\": \"239093847612784787\",\n" +
                    "    \"bz\": \"测试开票\",\n" +
                    "    \"kprq\": \"2023-11-01 12:30:24\",\n" +
                    "    \"tsyx\": \"113733111@qq.com\",\n" +
                    "    \"tssjh\": \"13711235617\",\n" +
                    "    \"fpmxList\": [\n" +
                    "      {\n" +
                    "        \"mxxh\": \"1\",\n" +
                    "        \"dylzfpmxxh\": \"\",\n" +
                    "        \"hwhyslwfwmc\": \"*电脑*联想电脑\",\n" +
                    "        \"spfwjc\": \"电脑\",\n" +
                    "        \"xmmc\": \"联想电脑\",\n" +
                    "        \"ggxh\": \"拯救者Y9000P\",\n" +
                    "        \"dw\": \"台\",\n" +
                    "        \"sl\": \"1\",\n" +
                    "        \"dj\": \"8500.00\",\n" +
                    "        \"je\": \"8500.00\",\n" +
                    "        \"slv\": \"13.00\",\n" +
                    "        \"se\": \"1105.00\",\n" +
                    "        \"hsje\": \"8500.00\",\n" +
                    "        \"kce\": \"7395.00\",\n" +
                    "        \"sphfwssflhbbm\": \"310012121312123\",\n" +
                    "        \"fphxz\": \"00\",\n" +
                    "        \"yhzcbs\": \"01\"\n" +
                    "      },\n" +
                    "      {\n" +
                    "        \"mxxh\": \"2\",\n" +
                    "        \"dylzfpmxxh\": \"\",\n" +
                    "        \"hwhyslwfwmc\": \"*手机*联想手机\",\n" +
                    "        \"spfwjc\": \"手机\",\n" +
                    "        \"xmmc\": \"苹果手机\",\n" +
                    "        \"ggxh\": \"iphoneXr\",\n" +
                    "        \"dw\": \"台\",\n" +
                    "        \"sl\": \"1\",\n" +
                    "        \"dj\": \"6999.00\",\n" +
                    "        \"je\": \"6999.00\",\n" +
                    "        \"slv\": \"13.00\",\n" +
                    "        \"se\": \"909.87\",\n" +
                    "        \"hsje\": \"6999.00\",\n" +
                    "        \"kce\": \"6099.13\",\n" +
                    "        \"sphfwssflhbbm\": \"310012121312124\",\n" +
                    "        \"fphxz\": \"00\",\n" +
                    "        \"yhzcbs\": \"01\"\n" +
                    "      }\n" +
                    "    ],\n" +
                    "    \"fjysList\": [\n" +
                    "      {\n" +
                    "        \"fjysmc\": \"附加要素名称1\",\n" +
                    "        \"fjyslx\": \"附加要素类型1\",\n" +
                    "        \"fjysz\": \"附加要素值200\"\n" +
                    "      },\n" +
                    "      {\n" +
                    "        \"fjysmc\": \"附加要素名称2\",\n" +
                    "        \"fjyslx\": \"附加要素类型2\",\n" +
                    "        \"fjysz\": \"附加要素值300\"\n" +
                    "      }\n" +
                    "    ]\n" +
                    "  }\n" +
                    "}";

            long startDetail = System.currentTimeMillis();
            // 写入请求正文
            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(requestBody.getBytes());
            outputStream.flush();

            // 发送请求并获取响应
            int responseCode = connection.getResponseCode();

            // 解析响应
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuilder response = new StringBuilder();

            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();

            // 输出响应
            System.out.println("响应代码: " + responseCode);
            System.out.println("响应体: " + response.toString());

            //调用查询发票接口
            getInvoice(num);

            //获取pdf文件
            //String getPdfUrl = headUrl + "/api/question/invoice/getInvoiceUploadResult";
            long endDetail = System.currentTimeMillis();
            System.out.println(num + "耗时：" + (endDetail - startDetail) + "ms");
            if (connection != null) {
                connection.disconnect();
            }


            long end = System.currentTimeMillis();
            System.out.println("总计耗时：" + (end - start) + "ms");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void getInvoice(int startNbr) {
        String headUrl = "http://114.116.222.38:32263";
        //headUrl = "http://127.0.0.1:8060";
        String getInvoiceUrl = headUrl + "/api/question/invoice/getInvoiceUploadResult";
        String requestBody = "{\n" +
                "  \"sllsh\":\"" + startNbr + "\"\n" +
                "}";

        // 创建URL对象
        URL url = null;
        try {
            long startDetail = System.currentTimeMillis();
            url = new URL(getInvoiceUrl);
            // 创建HttpURLConnection对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 设置请求方法为POST
            connection.setRequestMethod("POST");
            // 添加Token到请求头
            connection.setRequestProperty("Authorization", token);

            // 设置其他请求头参数
            connection.setRequestProperty("Content-Type", "application/json");

            // 允许写入请求正文
            connection.setDoOutput(true);

            // 写入请求正文
            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(requestBody.getBytes());
            outputStream.flush();

            // 发送请求并获取响应
            int responseCode = connection.getResponseCode();

            // 解析响应
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuilder response = new StringBuilder();

            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();

            // 输出响应
            System.out.println("Response Code: " + responseCode);
            System.out.println("Response Body: " + response.toString());

            long endDetail = System.currentTimeMillis();
            System.out.println(startNbr + "查询并下载pdf耗时：" + (endDetail - startDetail) + "ms");
            if (connection != null) {
                connection.disconnect();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
